package com.hitnet.hitnet.pic;



import android.animation.AnimatorSet;
import android.animation.ObjectAnimator;
import android.annotation.SuppressLint;
import android.content.Context;
import android.graphics.Bitmap;
import android.os.Handler;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.CompoundButton.OnCheckedChangeListener;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.TextView;

import com.hitnet.hitnet.R;

import java.util.List;


public class ChildAdapter extends BaseAdapter{

    private GridView mGridView;
    private List<String> list;//
    protected static LayoutInflater mInflater;
    private int totalSelected;//
    private Handler handler;
    private Button complete;
    private TextView dirNameText;
    
    public ChildAdapter(Context context, List<String> list, GridView mGridView, Handler handler, Button complete) {
    	totalSelected = NativeImageLoader.getSelectSize();
    	this.list = list;
        this.mGridView = mGridView;
        this.handler = handler;
        this.complete = complete;
        mInflater = LayoutInflater.from(context);
    }

    public ChildAdapter(Context context, List<String>list , GridView mGridView,Handler handler,Button complete,TextView mDirNameText){
        totalSelected = NativeImageLoader.getSelectSize();
        this.list = list;
        this.mGridView = mGridView;
        this.handler = handler;
        this.complete = complete;
        mInflater = LayoutInflater.from(context);
        dirNameText = mDirNameText;

        complete.setText("upload");
    }


    public int getCount() {
        return list.size();
    }

    public Object getItem(int position) {
        return list.get(position);
    }


    public long getItemId(int position) {
        return position;
    }

    public View getView(final int position, View convertView, ViewGroup parent) {
        final ViewHolder viewHolder;
        final String path = list.get(position);

        if (convertView == null) {
            convertView = mInflater.inflate(R.layout.grid_child_item, null);
            viewHolder = new ViewHolder();
            viewHolder.mImageView = (ImageView) convertView.findViewById(R.id.child_image);
            viewHolder.mCheckBox = (CheckBox) convertView.findViewById(R.id.child_checkbox);
            convertView.setTag(viewHolder);
        } else {
            viewHolder = (ViewHolder) convertView.getTag();
            viewHolder.mImageView.setImageResource(R.drawable.friends_sends_pictures_no);
        }
        viewHolder.mImageView.setTag(path);
//        viewHolder.mCheckBox.setChecked(NativeImageLoader.mSelectMap.containsKey(path) ? true : false);
        viewHolder.mCheckBox.setOnCheckedChangeListener(new OnCheckedChangeListener() {

            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {

//                if (!NativeImageLoader.mSelectMap.containsKey(path) || !NativeImageLoader.mSelectMap.get(path)) {
//                    if (totalSelected < 9) {
//                        addAnimation(viewHolder.mCheckBox);
//                    }
//                }
            	
                if (totalSelected < 9) {
                    if (isChecked) {
//                        NativeImageLoader.mSelectMap.put(path, isChecked);
                    	NativeImageLoader.mSelectList.add(path);
                        totalSelected = NativeImageLoader.getSelectSize();
                    } else {
                        NativeImageLoader.mSelectList.remove(path);
                    	totalSelected = NativeImageLoader.getSelectSize();
                    }
                } else {
                    if (isChecked) {
                        viewHolder.mCheckBox.setChecked(false);
                        handler.sendEmptyMessage(-1);
                    } else {
                        viewHolder.mCheckBox.setChecked(isChecked);
                        NativeImageLoader.mSelectList.remove(path);
                        totalSelected = NativeImageLoader.getSelectSize();
                    }
                }
                changeCompleteText();
            }
        });


        //NativeImageLoader
        Bitmap bitmap = NativeImageLoader.getInstance().loadNativeImage(position,path, viewHolder.mImageView.getWidth(), 100 , new NativeImageLoader.NativeImageCallBack() {

            public void onImageLoader(Bitmap bitmap, String path) {
                ImageView mImageView = (ImageView) mGridView.findViewWithTag(path);
                if (bitmap != null && mImageView != null) {
                    mImageView.setImageBitmap(bitmap);
                }
            }
        });

        if (bitmap != null) {
            viewHolder.mImageView.setImageBitmap(bitmap);
        } else {
            viewHolder.mImageView.setImageResource(R.drawable.friends_sends_pictures_no);
        }

        return convertView;
    }
    
    private void changeCompleteText(){
    	if(totalSelected == 0){
//
    	}else{
//
            dirNameText.setText("Select("+totalSelected+"/"+"9)");
    	}

    }
//    private void removePath(String path){
//    	NativeImageLoader.mSelectList.contains(path);
//    }

    /**
     * CheckBox
     *
     * @param view
     */
    @SuppressLint("NewApi")
	private void addAnimation(View view) {
        float[] vaules = new float[]{0.5f, 0.6f, 0.7f, 0.8f, 0.9f, 1.0f, 1.1f, 1.2f, 1.3f, 1.25f, 1.2f, 1.15f, 1.1f, 1.0f};
        AnimatorSet set = new AnimatorSet();
        set.playTogether(ObjectAnimator.ofFloat(view, "scaleX", vaules),
                ObjectAnimator.ofFloat(view, "scaleY", vaules));
        set.setDuration(150);
        set.start();
    }


    public class ViewHolder {
        public ImageView mImageView;
        public CheckBox mCheckBox;
    }


	public void setmGridView(GridView mGridView) {
		this.mGridView = mGridView;
	}
	public void setList(List<String> list) {
		this.list = list;
	}
 	public void setHandler(Handler handler) {
		this.handler = handler;
	}

}
